<?php
// +----------------------------------------------------------------------
// | 知识星球[基于ThinkPHP5开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 abcblockchain.io
// +----------------------------------------------------------------------
// | 后台管理使用了HisiPHP
// +----------------------------------------------------------------------
// | Author: abcblockchain.io
// +----------------------------------------------------------------------
namespace app\common\model;

use think\Model;
use think\Db;
use app\common\model\Comments;

/**
 * 主题模型
 * @package app\common\model
 */
class GroupPosts extends Model
{
    // 定义时间戳字段名
    protected $createTime = 'ctime';
    protected $updateTime = false;

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;


    /**
     * 创建主题
     * @param array $data
     * @author jyx
     * @return stirng|array
     */
    public function createPosts($data = [], $login = true)
    {
        $map = [];
        $map['group_id'] = $data['group_id'];
        $map['user_id'] = $data['user_id'];
        $map['body'] = $data['body'];
        $map['type'] = isset($data['type']) ? $data['type'] : 1;
        $map['q_user_id'] = isset($data['q_user_id']) ? $data['q_user_id'] : 0;
        $map['an_post_id'] = isset($data['an_post_id']) ? $data['an_post_id'] : 0;
        $map['images'] = isset($data['images']) ? $data['images'] : '';

        $res = $this->validate('GroupPosts.create')->isUpdate(false)->save($map);
        if (!$res) {
            return false;
        } else {
            Db::name('Group')->where('id', $data['group_id'])->setInc('posts_count');
            return true;
        }
    }

    //关联用户表
    public function hasUser()
    {
        return $this->hasOne('Users', 'id', 'user_id');
    }

    //关联星球表
    public function hasGroup()
    {
        return $this->hasOne('Group', 'id', 'group_id');
    }

    /**
     * 获取星球下主题
     * @author
     * @param $groupId
     */
    public function postList($groupId, $startNo = 0, $limit = 6, $isEssences = false)
    {
        $map['p.disabled'] = 1;
        $map['p.group_id'] = $groupId;
        if ($isEssences) {
            $map['p.is_essence'] = 1;
        }
        $startNo = $startNo * $limit;
        $postList = $this->alias('p')->join('users u', 'p.user_id=u.id')->where($map)->field('p.id,p.user_id,p.body,p.images,p.likes_count,p.comments_count,p.ctime,u.nick,u.avatar')->limit($startNo, $limit)->order('ctime desc')->select();
        if ($postList) {
            $comment = new Comments();
            foreach ($postList as $item => $v) {
                $imgList = array();
                $likeList = $this->likeUsers($v['id']);
                $likeUid = $this->likeUserId($v['id']);
                if ($v['images']) {
                    $imgList = json_decode(htmlspecialchars_decode($v['images']), true);
                    if ($imgList) {
                        foreach ($imgList as $imgk => $imgv) {
                            $imgInfo = [];
                            if (is_file(ROOT_PATH . $imgv['img'])) {
                                $imgInfo = getimagesize(ROOT_PATH . $imgv['img']);
                                $imgList[$imgk]['src'] = "http://3.112.84.186" . $imgv['img'];
                                $imgList[$imgk]['msrc'] = "http://3.112.84.186" . $imgv['img'];
                                $imgList[$imgk]['w'] = $imgInfo[0];
                                $imgList[$imgk]['h'] = $imgInfo[1];
                            }
                        }
                    }
                }
                $postList[$item]['images'] = $imgList;
                $postList[$item]['likeList'] = $likeList ? $likeList : array();
                $postList[$item]['likeUid'] = $likeUid;
                $commentList = $comment->getReply($v['id']);
                $postList[$item]['commentList'] = $commentList ? $commentList : array();
            }
            return $postList;
        } else {
            return false;
        }
    }

    /**
     * 获取帖子详情
     * @author
     * @param $postId
     * @return array|bool|false|\PDOStatement|string|Model
     */
    public function postDetail($postId)
    {
        $map['p.disabled'] = 1;
        $map['p.id'] = $postId;
        $postInfo = $this->alias('p')->join('users u', 'p.user_id=u.id')->where($map)->field('p.id,p.user_id,p.body,p.images,p.likes_count,p.comments_count,p.ctime,u.nick,u.avatar')->find();
        if ($postInfo) {
            $comment = new Comments();
            $imgList = array();
            $likeList = $this->likeUsers($postInfo['id']);
            $likeUid = $this->likeUserId($postInfo['id']);
            if ($postInfo['images']) {
                $imgList = json_decode(htmlspecialchars_decode($postInfo['images']), true);
                if ($imgList) {
                    foreach ($imgList as $imgk => $imgv) {
                        $imgInfo = [];
                        if (is_file(ROOT_PATH . $imgv['img'])) {
                            $imgInfo = getimagesize(ROOT_PATH . $imgv['img']);
                            $imgList[$imgk]['src'] = "http://3.112.84.186" . $imgv['img'];
                            $imgList[$imgk]['msrc'] = "http://3.112.84.186" . $imgv['img'];
                            $imgList[$imgk]['w'] = $imgInfo[0];
                            $imgList[$imgk]['h'] = $imgInfo[1];
                        }
                    }
                }
            }
            $postInfo['images'] = $imgList;
            $postInfo['likeList'] = $likeList ? $likeList : array();
            $postInfo['likeUid'] = $likeUid;
            $commentList = $comment->getReply($postInfo['id']);
            $postInfo['commentList'] = $commentList ? $commentList : array();

            return $postInfo;
        } else {
            return false;
        }
    }


    /**
     * 点赞人数
     * @author
     * @param $postId
     */
    public function likeUsers($postId)
    {
        $map['lk.status'] = 1;
        $map['lk.likeable_type'] = 1;
        $map['lk.likeable_id'] = $postId;
        $likeUsers = Db::name('likes')->alias('lk')->join('users u', 'lk.user_id=u.id')->where($map)->field('lk.user_id,u.nick')->select();
        return $likeUsers;
    }

    /**
     * 点赞人Id
     * @author
     * @param $postId
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function likeUserId($postId)
    {
        $map['status'] = 1;
        $map['likeable_type'] = 1;
        $map['likeable_id'] = $postId;
        $likeUsers = Db::name('likes')->where($map)->field('user_id')->select();
        if ($likeUsers) {
            $likeUsers = array_reduce($likeUsers, function ($result, $value) {
                return array_merge($result, array_values($value));
            }, array());
            $likeUsers = implode(',', $likeUsers);
            $likeUsers = ',' . $likeUsers;
        } else {
            $likeUsers = '';
        }
        return $likeUsers;
    }

    /**
     * 发现页帖子
     * @author
     * @param $groupIdStr
     * @param int $startNo
     * @param int $limit
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function findPost($groupIdStr, $startNo = 0, $limit = 10)
    {
        $map['p.disabled'] = 1;
        $map['p.group_id'] = array('in', $groupIdStr);
        $startNo = $startNo * $limit;
        $postList = $this->alias('p')->join('users u', 'p.user_id=u.id')->join('group g', 'g.id=p.group_id')->where($map)->field('p.id,p.user_id,p.body,p.images,p.likes_count,p.comments_count,p.group_id,p.ctime,u.nick,u.avatar,g.name')->limit($startNo, $limit)->order('ctime desc')->select();
        if ($postList) {
            foreach ($postList as $item => $v) {
                $imgList = array();
                if ($v['images']) {
                    $imgList = json_decode(htmlspecialchars_decode($v['images']), true);
                    if ($imgList) {
                        foreach ($imgList as $imgk => $imgv) {
                            $imgInfo = [];
                            if (is_file(ROOT_PATH . $imgv['img'])) {
                                $imgInfo = getimagesize(ROOT_PATH . $imgv['img']);
                                $imgList[$imgk]['src'] = "http://3.112.84.186" . $imgv['img'];
                                $imgList[$imgk]['msrc'] = "http://3.112.84.186" . $imgv['img'];
                                $imgList[$imgk]['w'] = $imgInfo[0];
                                $imgList[$imgk]['h'] = $imgInfo[1];
                            }
                        }
                    }
                }
                $postList[$item]['images'] = $imgList;
            }
        }
        return $postList ? $postList : array();
    }

    /**
     * 搜索主题
     * @author
     * @param $searchStr
     * @param string $userGroup
     * @param int $type
     * @param int $startNo
     * @param int $limit
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function searchPost($searchStr, $userGroup = '', $type = 0, $startNo = 0, $limit = 10)
    {
        $map['p.disabled'] = 1;
        $map['p.status'] = 1;
        if ($type == 0) {
            $map['p.group_id'] = array('in', $userGroup);
            $map['p.body'] = array('like', "%$searchStr%");
        } else {
            $map['p.group_id'] = array('not in', $userGroup);
            $map['p.body'] = array('like', "%$searchStr%");
        }
        $startNo = $startNo * $limit;
        $postList = $this->alias('p')->join('users u', 'p.user_id=u.id')->join('group g', 'g.id=p.group_id')->where($map)->field('p.id,p.user_id,p.group_id,p.body,p.images,p.likes_count,p.comments_count,p.ctime,u.nick,u.avatar,g.name')->limit($startNo, $limit)->order('ctime desc')->select();
        return $postList ? $postList : array();
    }

    /**
     * 获取用户班级内主题
     * @author
     * @param $userId
     * @param $groupId
     * @param int $startNo
     * @param int $limit
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function userGroupPost($userId, $groupId, $startNo = 0, $limit = 6)
    {
        $map['user_id'] = $userId;
        $map['group_id'] = $groupId;
        $map['disabled'] = 1;
        $map['status'] = 1;
        $startNo = $startNo * $limit;
        $postList = $this->where($map)->limit($startNo, $limit)->order('ctime desc')->select();
        if ($postList) {
            foreach ($postList as $item => $v) {
                $imgList = array();
                if ($v['images']) {
                    $imgList = json_decode(htmlspecialchars_decode($v['images']), true);
                    if ($imgList) {
                        foreach ($imgList as $imgk => $imgv) {
                            $imgInfo = [];
                            if (is_file(ROOT_PATH . $imgv['img'])) {
                                $imgInfo = getimagesize(ROOT_PATH . $imgv['img']);
                                $imgList[$imgk]['src'] = "http://3.112.84.186" . $imgv['img'];
                                $imgList[$imgk]['msrc'] = "http://3.112.84.186" . $imgv['img'];
                                $imgList[$imgk]['w'] = $imgInfo[0];
                                $imgList[$imgk]['h'] = $imgInfo[1];
                            }
                        }
                    }
                }
                $postList[$item]['images'] = $imgList;
            }
        }
        return $postList ? $postList : array();
    }
}